% function [globalbest_x,DE_best]=pso(data)
% globalbest_faval = 0;
% particlesize=50;%粒子群规模
% c1=2;%每个粒子的个体学习因子，加速度常数
% c2=2;%每个粒子的社会学习因子，加速度常数
% w=0.6;%惯性因子
% v(:,1)=round(2*rand(particlesize,1));%粒子飞翔速度
% v(:,2)=round(10*rand(particlesize,1));
% x(:,1)=round(5*rand(particlesize,1))+2;
% x(:,2)=round(100*rand(particlesize,1))+20;
% x=limit(x);
% %定义适应度函数
% %data=vmddata(1:200,:);
% %data=reshape(data,100,1200);
% fitnes=zeros(1,particlesize);
% f=zeros(1,particlesize);
% DE=zeros(particlesize,200);
% for i=1:particlesize
%     [fitnes(i),DE(i,:)]=fitness(data,x(i,1),x(i,2));
% end
% for i=1:particlesize
%     f(i)=fitnes(i);
% end
% personalbest_x=x;
% personalbest_faval=f;
% [~,i]=min(personalbest_faval);
% globalbest_x=personalbest_x(i,:);
% k=1;
% while (k<=100)
%     for i=1:particlesize
%         [fitnes(i),DE(i,:)]=fitness(data,x(i,1),x(i,2));
%         f(i)=fitnes(i);
%         if f(i)<personalbest_faval(i)
%             personalbest_faval(i)=f(i);
%             personalbest_x(i,:)=x(i,:);
%         end
%     end
%     E=globalbest_faval;
%     [globalbest_faval,i]=min(personalbest_faval);
%     globalbest_x=personalbest_x(i,:);
%     DE_best=DE(i,:);
%     for i=1:particlesize
%         v(i,:)=round(w*v(i,:)+c1*rand*(personalbest_x(i,:)-x(i,:))...
%             +c2*rand*(globalbest_x-x(i,:)));
%         x(i,:)=x(i,:)+v(i,:);
%         x=limit(x);
%     end
%     %ff(k)=globalbest_faval;
%     if abs(globalbest_faval-E) <= 0.1
%         break
%     end
%     k=k+1;
% end
% end
% function x=limit(x)
% [m,~]=size(x);
% for i=1:m
%     if x(i,1)<3
%         x(i,1)=3;
%     elseif x(i,1)>10
%         x(i,1)=10;
%     end
%     if x(i,2)<10
%         x(i,2)=10;
%     elseif x(i,2)>120
%         x(i,2)=120;
%     end
% end
% end

function [globalbest_x,DE_best]=pso(data)
E=0.001;
particlesize=50;%粒子群规模
c1=2;%每个粒子的个体学习因子，加速度常数
c2=2;%每个粒子的社会学习因子，加速度常数
w=0.6;%惯性因子
v(:,1)=round(2*rand(particlesize,1));%粒子飞翔速度
v(:,2)=round(10*rand(particlesize,1));
x(:,1)=round(10*rand(particlesize,1))+2;
x(:,2)=round(100*rand(particlesize,1))+10;
%定义适应度函数
%data=X118_FE_time(1:120000);
%data=reshape(data,100,1200);
fitnes=zeros(1,particlesize);
f=zeros(1,particlesize);
DE=zeros(particlesize,200);
for i=1:particlesize
    [fitnes(i),DE(i,:)]=fitness(data,x(i,1),x(i,2));
end
for i=1:particlesize
    f(i)=fitnes(i);
end
personalbest_x=x;
personalbest_faval=f;
[~,i]=min(personalbest_faval);
globalbest_x=personalbest_x(i,:);
k=1;
while (k<=10)
    for i=1:particlesize
        [fitnes(i),DE(i,:)]=fitness(data,x(i,1),x(i,2));
        f(i)=fitnes(i);
        if f(i)<personalbest_faval(i)
            personalbest_faval(i)=f(i);
            personalbest_x(i,:)=x(i,:);
        end
    end
    [globalbest_faval,i]=min(personalbest_faval);
    globalbest_x=personalbest_x(i,:);
    DE_best=DE(i,:);
    for i=1:particlesize
        v(i,:)=round(w*v(i,:)+c1*rand*(personalbest_x(i,:)-x(i,:))...
            +c2*rand*(globalbest_x-x(i,:)));
        x(i,:)=x(i,:)+v(i,:);
        x=limit(x);
    end
    %ff(k)=globalbest_faval;
    if globalbest_faval<E
        break
    end
    k=k+1;
end
end
function x=limit(x)
[m,~]=size(x);
for i=1:m
    if x(i,1)<2
        x(i,1)=2;
    elseif x(i,2)<10
          x(i,2)=10;
    end
    if x(i,1)>15
        x(i,1)=15;
    elseif x(i,2)>120
          x(i,2)=120;
    end
end
end


